<?php //strip
/**
 * Newest items
 * 
 * @author Ondrej Donek <ondrejd@gmail.com>
 * @version 0.1
 */
class odEshopNewestItemsWidget extends WP_Widget
{
  
  /**
   * Constructor.
   * 
   * @return void
   */
  function odEshopNewestItemsWidget() 
  {
	// Base widget properties
	$id_base = 'odeshopnewestitemswidget-widget';
	$name = 'DVD novinky';
	$description = 'Seznam nejnovějších DVD';
	
	// Widget options
	$widget_options = array('classname'   => 'odEshopNewestItemsWidget', 
	                        'description' => $description);
	
	// Widget control options
	$control_options = array('width'   => 200, 
	                         'height'  => 350, 
							 'id_base' => $id_base);
	
	// Create the widget
	parent::WP_Widget($id_base, $name, $widget_options, $control_options);
  }
  
  /**
   * Create widget.
   * 
   * @param array $args
   * @param array $instance
   * @return void
   */
  function widget($args, $instance) 
  {
	extract($args);

	// User-selected settings
	$title = apply_filters('widget_title', $instance['title']);
	$maxcount = (int) $instance['maxcount'];
	$category = (int) $instance['use_category'];

	// Before widget (defined by theme)
	echo $before_widget;
	
	// Render our widget
	global $od_eshop_plugin;
	$items = odWpEshopPluginDbModel::getLatestEshopItems($maxcount, $category);
	$i = 0;	
?>
	<h1><?php echo $title;?></h1>
	<h2><span><a href="<?php echo $od_eshop_plugin->get_main_page_url();?>">zobrazte si celý katalog</a></span>Z posledních přidaných novinek vybíráme:</h2>
<?php foreach($items as $item):?>
		<?php if($i == 0):?>
		<span class="karty">
		<?php endif;?>
			<span class="karta">
				<span class="obrazek">
					<a href="<?php echo $item['detail_url'];?>"><img src="<?php echo $item['image_url3'];?>" alt="<?php echo $item['title'];?>" /></a>
				</span>
				<span class="text">
					<b><a href="<?php echo $item['detail_url'];?>"><?php echo $item['item_title'];?></a></b>
					<i>Kategorie:</i> <?php echo $item['genre_categories'];?><br />
					<i>Jazyk:</i> <?php echo $item['item_language'];?><br />
					<i>Titulky:</i> <?php echo $item['item_subtitles'];?>
				</span>
			</span>
		<?php if($i == 2):?>
			<?php $i = 0;?>
		</span>
		<?php else:?>
			<?php $i = $i + 1;?>
		<?php endif;?>
<?php endforeach;?>
<?php
	
	// After widget (defined by theme)
	echo $after_widget;
  }
  
  /**
   * Updates and returns plugin instance.
   * 
   * @param array $new_instance
   * @param array $old_isntance
   * @return array
   */
  function update($new_instance, $old_instance) 
  {
	$instance = $old_instance;

	// Strip tags (if needed) and update the widget settings
	$instance['title'] = $new_instance['title'];
	$instance['maxcount'] = $new_instance['maxcount'];
	$instance['use_category'] = $new_instance['use_category'];

	return $instance;
  }
  
  /**
   * Renders widget form for the WordPress admin
   * 
   * @param array $instance
   * @return void
   */
  function form($instance) 
  {
	// Use default widget settings.
	$defaults = array('title'        => 'DVD novinky', 
					  'maxcount'     => 6,
					  'use_category' => 0);
	$instance = wp_parse_args((array) $instance, $defaults); 
?>
	<p>
	  <label for="<?php echo $this->get_field_id('title');?>">Název:</label>
	  <input type="text" id="<?php echo $this->get_field_id('title');?>" name="<?php echo $this->get_field_name('title');?>" value="<?php echo $instance['title'];?>" style="width:100%;"/>
	</p>
	<p>
	  <label for="<?php echo $this->get_field_id('maxcount');?>">Počet položek:</label>
	  <input type="text" id="<?php echo $this->get_field_id('maxcount');?>" name="<?php echo $this->get_field_name('maxcount');?>" value="<?php echo $instance['maxcount'];?>" style="width:100%;"/>
	</p>
	<p>
	  <label for="<?php echo $this->get_field_id('use_category');?>">Zobrazit vybranou kategorii:</label>
	  <input type="text" id="<?php echo $this->get_field_id('use_category');?>" name="<?php echo $this->get_field_name('use_category');?>" value="<?php echo $instance['use_category'];?>"/>
	</p>
	<p>
	    Pokud je políčko vybrané kategorie rovno <strong>0</strong> budou 
    	zobrazeny nejnověji přidané výrobky ze všech kategorií.
    </p>
<?php
  }
  
}

